package com.example.user

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.RadioButton
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.lib_base.mvvm.BaseViewActivity
import com.example.user.databinding.ActivityPayamBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PayamActivity : BaseViewActivity<ActivityPayamBinding>() {
    private var memberId: String? = null // 会员唯一标识
    private var memberName: String? = null // 会员名称
    // 支付宝支付结果回调标识
    override fun initView() {
        // 获取从上一页传递的会员信息
        memberId = intent.getStringExtra("MEMBER_ID")
        memberName = intent.getStringExtra("MEMBER_NAME")
        // 确认支付按钮点击事件
        binding.pay.setOnClickListener {
            validateAndPay()
        }
        binding.ivBack.setOnClickListener {
            finish()
        }
    }

    override fun initData() {

    }

    /**
     * 验证选择并处理支付
     */
    private fun validateAndPay() {
        // 获取选中的RadioButton
        val selectedId = binding.rgMemberOptions.checkedRadioButtonId

        if (selectedId == -1) {
            // 未选择任何选项
            showAlertDialog("温馨提示", "请选择一项会员升级方案")
        } else {
            // 仅选择一项，处理支付
            val selectedOption = findViewById<RadioButton>(selectedId).text.toString()

            // PayamActivity 支付成功后
            val intent = Intent(this, UserActivity::class.java)
            intent.putExtra("member_status", selectedOption) // selectedStatus为你选中的会员状态文字
            startActivity(intent)
            processPayment(selectedOption)


            finish()

        }
    }

    /**
     * 显示提示对话框
     */
    private fun showAlertDialog(title: String, message: String) {
        AlertDialog.Builder(this)
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
            }
            .create()
            .show()
    }

    /**
     * 处理支付逻辑
     */
    private fun processPayment(option: String) {
        // 模拟支付过程
        // 实际项目中这里会调用支付SDK或后端接口
        Toast.makeText(this, "支付成功！您已升级至$option", Toast.LENGTH_LONG).show()

        // 支付成功后可关闭当前页面或跳转到成功页面
        // finish()
    }

}